<%--
  @author Kusal Samarakoon
  @author Supun Viraj Rathnayaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery.jqplot.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jquery.jqplot.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.pieRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.barRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.categoryAxisRenderer.js"></script>


<script type="text/javascript">

    $(function () {
        $('.land-table').html(initTableHead());
        $('#chartdiv').hide();
    });


    function generateStaicstics(){
        var id;
        var mode;
        var type = "landStat";
        var from = $('input#startDate').val().split("-");
        var to  = $('input#endDate').val().split("-");
        var landAppStateId = $('select#landState').attr("value");
        var fromDate = new Date(from[0],(from[1]-1),from[2]).getTime();
        var toDate = new Date(to[0],(to[1]-1),to[2]).getTime();
        var migId = $('select#migrationState').attr("value");

        //alert("From : "+fromDate.toJSON()+" To : "+toDate.toJSON()+" State : "+landStateId);

        if($('select#gnDivisionId').attr("value")>0){
            id = $('select#gnDivisionId').attr('value');
            mode = "byGNDivision";
        }else if($('select#dsDivisionId').attr("value")>0){
            id = $('select#dsDivisionId').attr("value");
            mode = "byDSDivision";
        }else if($('select#districtId').attr("value")>0){
            id=$('select#districtId').attr("value");
            mode = "byDistrict";
        }else{
            id =  $('select#provinceId').attr("value");
            mode = "byProvince";
        }
        $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id:id, mode:mode, type:type, fromDate:fromDate, toDate:toDate, landState:landAppStateId, migState:migId}, function(data,status) {
           //alert(status);
           //alert(JSON.stringify(data));
           loadLandList(data.landList);
        });
    }

    function loadLandList(landInfo) {
        var opt = '';
        var total = 0;
        opt += initTableHead();
        opt += '<tbody>';

        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                opt += '<tr>' +
                        '<td class="center-align">' + getApprovalStateName(landInfo[i][0]) + '</td>' +
                        '<td class="center-align">' + landInfo[i][1] + '</td>' +
                        '</tr>';
                total +=landInfo[i][1];
            }
        }else{
            opt+='<tr>'+
                    '<td colspan="7" class="center-align">' +
                    $('#no-record').val() +
                    '</td>' +
                    '</tr>';
        }

        opt += '</tbody>';
        if(landInfo.length > 1){
            opt+= initTableFoot(total);
        }
        $('.land-table').html(opt);
        if(landInfo.length > 0){
            $('#chartdiv').slideDown("slow",function(){
                showPieChart(landInfo);
            });
        }else{

            $('#chartdiv').slideUp("slow",function(){
                $('#chartdiv').empty();
            });
        }

    }

    function initTableHead(){
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#land_status').val() + '</th>' +
                '<th class="center-align">' + $('#no_of_lands').val() + '</th>' +
                '</tr>' +
                '</thead>';
        return opt;
    }

    function initTableFoot(total){
        var opt = '';
        opt += '<tfoot>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#all_lands_sum').val() + '</th>' +
                '<th class="center-align">' + total + '</th>' +
                '</tr>' +
                '</tfoot>';
        return opt;
    }

    function showPieChart(landInfo){

        var data = new Array();

        for (var i = 0; i < landInfo.length; i++) {
            data.push([getApprovalStateName(landInfo[i][0]),landInfo[i][1]]);
        }
        var plot1 = jQuery.jqplot ('chartdiv', [data],
                {
                    seriesDefaults: {
                        renderer: jQuery.jqplot.PieRenderer,
                        rendererOptions:{
                            showDataLabels: true
                        }
                    },
                    legend: { show:true, location: 'e', fontSize: 20 }
                }
        );
    }

    function getApprovalStateName(name){
       var stateName;
       if(name==="REJECTED"){
          stateName = $('#rejected').val();
       }else if(name==="APPROVED"){
          stateName = $('#approved').val();
       }else if(name==="PENDING"){
          stateName = $('#pending').val();
       }else if(name==="DATA_ENTRY"){
          stateName = $('#data_entry').val();
       }
        return stateName;
    }

    $(function () {
        $('select#provinceId').bind('change', function (evt1) {
            var id = $("select#provinceId").attr("value");
            $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 1},
                    function (data) {
                        var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                        var ds = data.districtsList;
                        for (var i = 0; i < ds.length; i++) {
                            options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                        }
                        $("select#districtId").html(options1);
                        var options2 = '<option value="0">' + $('#all').val() + '</option> ';
                        $("select#dsDivisionId").html(options2);
                        $("select#gnDivisionId").html(options2);
                    });

        });
    });

    $(function () {
        $('select#districtId').bind('change', function (evt1) {
            var id = $("select#districtId").attr("value");
            $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 2},
                    function (data) {
                        //var options1 = '<option value="0"> - Select  - </option> ';
                        var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                        var ds = data.dsDivisionList;
                        for (var i = 0; i < ds.length; i++) {
                            options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                        }
                        $("select#dsDivisionId").html(options1);
                        var options2 = '<option value="0">' + $('#all').val() + '</option> ';
                        $("select#gnDivisionId").html(options2);
                    });

        });
    });

    $(function () {
        $('select#dsDivisionId').bind('change', function (evt1) {
            var id = $("select#dsDivisionId").attr("value");
            $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 3},
                    function (data) {
                        //var options1 = '<option value="0"> - Select  - </option> ';
                        var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                        var ds = data.gnDivisionList;
                        for (var i = 0; i < ds.length; i++) {
                            options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                        }
                        $("select#gnDivisionId").html(options1);
                    });

        });
    });

</script>

<div id="land-stat-module-home-title" class="module-home-title">
    <s:a href="home.do"><img src="${pageContext.request.contextPath}/images/home.png"
                             alt="Home"><span><s:label
            value="%{getText('home.label')}"/></span></s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="statisticsModule.do"><img src="${pageContext.request.contextPath}/images/stat.png"
                                         alt="View Statistics"><span><s:label
            value="%{getText('statisticsModule.label')}"/></span></s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="landStat.do"><img src="${pageContext.request.contextPath}/images/stat.png"
                                 alt="View Land Statistics"><span><s:label
            value="%{getText('landStat.label')}"/></span></s:a>
    <div id="back-button" align="right">
          <s:form><s:submit value="%{getText('back_btn.label')}" action="statisticsModule"/></s:form>
     </div>
</div>

<table>
    <tr>
        <td colspan="2" class="left-align">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </td>
    </tr>
</table>
<div id="land-stat-module-home" class="module-home">

    <h2><s:label value="%{getText('land_statistics.label')}"/></h2>

    <table>
        <tr>
            <td class="left-align">
                <s:label value="%{getText('time_range.label')}"/>
            </td>
            <td class="left-align">
                <s:textfield cssClass="width-200-px" id="startDate" name="startDate" onclick="selectDate('startDate')"
                             onKeyPress="return dateNumbersOnly(event,true)"/>
            </td>
            <td class="left-align">
                <s:label value="%{getText('from.label')}"/>
            </td>
            <td class="left-align">
                <s:textfield cssClass="width-200-px" id="endDate" name="endDate" onclick="selectDate('endDate')"
                             onKeyPress="return dateNumbersOnly(event,true)" />
            </td>
            <td class="left-align">
                <s:label value="%{getText('to.label')}"/>
            </td>
        </tr>
        <tr>
            <td><s:label value="%{getText('land_status.label')}"/></td>
            <td><s:select id="landState" name="landState"
                          list="landStateList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-200-px"/></td>
            <td ><s:label value="%{getText('migration_state.label')}"/></td>
            <td colspan="2"><s:select id="migrationState" name="migrationState"
                                      list="migrationStateList" headerKey="0"
                                      headerValue="%{getText('all.label')}" cssClass="width-200-px"/></td>
        </tr>

        <tr>
            <td></td>
            <td colspan="2"><s:label value="%{getText('province.label')}"/></td>
            <td colspan="2"><s:label value="%{getText('district.label')}"/></td>
        </tr>
        <tr>
            <td></td>
            <td colspan="2" class="center-align">
                <s:if test="%{provinceList.size==1}">
                    <s:select id="provinceId" name="provinceId" list="provinceList" cssClass="width-250-px"/>
                </s:if>
                <s:else>
                    <s:select id="provinceId" name="provinceId" list="provinceList" headerKey="0"
                              headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                </s:else></td>
            <td colspan="2" class="center-align">
                <s:if test="%{districtList.size==1}">
                    <s:select id="districtId" name="districtName" list="districtList" cssClass="width-250-px"/>
                </s:if>
                <s:else>
                    <s:select id="districtId" name="districtName" list="districtList" headerKey="0"
                              headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                </s:else></td>
        </tr>
        <tr>
            <td></td>
            <td colspan="2"><s:label value="%{getText('ds_division.label')}"/></td>
            <td colspan="2"><s:label value="%{getText('gn_division.label')}"/></td>
        </tr>
        <tr>
            <td></td>
            <td colspan="2" class="center-align">
                <s:if test="%{dsDivisionList.size==1}">
                    <s:select id="dsDivisionId" name="dsDivisionId" list="dsDivisionList" cssClass="width-250-px"/>
                </s:if>
                <s:else>
                    <s:select id="dsDivisionId" name="dsDivisionId" list="dsDivisionList" headerKey="0"
                              headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                </s:else></td>
            <td colspan="2" class="center-align">
                <s:select id="gnDivisionId" name="gnDivisionId" list="gnDivisionList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-250-px"/></td>
        </tr>
        <tr>
            <td colspan="5" class="right-align"><s:submit onClick="generateStaicstics()" value="%{getText('search.label')}"/></td>
        </tr>
    </table>


    <table class="width-100 margin-10-auto table land-table">
    </table>

    <div id="chartdiv" style="height:500px;width:830px; "></div>

    <%-- Localization text for data tables --%>
    <s:hidden id="display" value="%{getText('display.label')}"/>
    <s:hidden id="records" value="%{getText('records.label')}"/>
    <s:hidden id="search" value="%{getText('search.label')}"/>
    <s:hidden id="no-record" value="%{getText('no_record.label')}"/>
    <s:hidden id="showing" value="%{getText('showing.label')}"/>
    <s:hidden id="to" value="%{getText('to.label')}"/>
    <s:hidden id="of" value="%{getText('of.label')}"/>
    <s:hidden id="entries" value="%{getText('entries.label')}"/>
    <s:hidden id="first" value="%{getText('first.label')}"/>
    <s:hidden id="last" value="%{getText('last.label')}"/>
    <s:hidden id="next" value="%{getText('next.label')}"/>
    <s:hidden id="previous" value="%{getText('previous.label')}"/>
    <s:hidden id="all" value="%{getText('all.label')}"/>
    <s:hidden id="rejected" value="%{getText('rejected.label')}"/>
    <s:hidden id="approved" value="%{getText('approve.label')}"/>
    <s:hidden id="pending" value="%{getText('pending.label')}"/>
    <s:hidden id="data_entry" value="%{getText('data_entry.label')}"/>
    <s:hidden id="land_status" value="%{getText('land_status.label')}"/>
    <s:hidden id="no_of_lands" value="%{getText('no_of_lands.label')}"/>
    <s:hidden id="all_lands_sum" value="%{getText('all_lands_sum.label')}"/>
</div>